﻿// ------------------------------------------------------------------------------
// <copyright file="DatePickerPage.xaml.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.  All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLibDemo.Examples.Controls
{
    using System;
    using System.ComponentModel;
    using StoreAppLib.Controls;
    using StoreAppLibDemo.Common;
    using Windows.UI.Popups;

    /// <summary>
    /// Date picker example page.
    /// </summary>
    public sealed partial class DatePickerPage : LayoutAwarePage, INotifyPropertyChanged
    {
        /// <summary>
        /// Holds current date
        /// </summary>
        private DateTime currentDate;

        /// <summary>
        /// Initializes a new instance of the <see cref="DatePickerPage"/> class"
        /// </summary>
        public DatePickerPage()
        {
            this.InitializeComponent();

            this.CurrentDate = DateTime.Now;
            this.DataContext = this;
        }

        /// <summary>
        /// Property changed event handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the date property.
        /// </summary>
        /// <value>
        /// The date property.
        /// </value>
        public DateTime CurrentDate
        {
            get
            {
                return this.currentDate;
            }

            set
            {
                if (!object.Equals(this.currentDate, value))
                {
                    this.currentDate = value;

                    PropertyChangedEventHandler handler = this.PropertyChanged;

                    if (handler != null)
                    {
                        handler.Invoke(this, new PropertyChangedEventArgs("CurrentDate"));
                        handler.Invoke(this, new PropertyChangedEventArgs("MinimumDate"));
                        handler.Invoke(this, new PropertyChangedEventArgs("MaximumDate"));
                    }
                }
            }
        }

        /// <summary>
        /// Gets the minimum date property.
        /// </summary>
        /// <value>
        /// The date property.
        /// </value>
        public DateTime MinimumDate
        {
            get
            {
                return DateTime.Now.AddMonths(-2);
            }
        }

        /// <summary>
        /// Gets the maximumDate date property.
        /// </summary>
        /// <value>
        /// The date property.
        /// </value>
        public DateTime MaximumDate
        {
            get
            {
                return DateTime.Now.AddMonths(+2);
            }
        }

        /// <summary>
        /// Clear date value. This can be done in three ways.
        /// 1. Resetting the date property value bound to the date control's Date property
        /// 2. Calling Clear method of the date control
        /// 3. Binding to ClearCommand property of the date control in XAML
        /// Second date control uses the ClearCommand property to clear
        /// </summary>
        /// <param name="sender">The sender</param>
        /// <param name="e">The argument</param>
        private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            ////this.CurrentDate = DateTime.MinValue;
            datePicker1.Clear();
        }

        /// <summary>
        /// Occurs when data changed. Set args.Cancel = true to cancel date change
        /// </summary>
        /// <param name="sender">The source</param>
        /// <param name="args">Date time change argument</param>
        private async void OnDatePicker1DateChanged(object sender, DateTimeEventArgs args)
        {
            if (chkCustomValidation.IsChecked == true)
            {
                if (args.DateTime < this.MinimumDate || args.DateTime > this.MaximumDate)
                {
                    args.Cancel = true;
                    MessageDialog errorMessage = new MessageDialog(string.Format("Selected date {0:dd MMMM yyyy} is invalid. Date should be between {1:dd MMMM yyyy} and {2:dd MMMM yyyy}", args.DateTime, this.MinimumDate, this.MaximumDate));
                    errorMessage.DefaultCommandIndex = 1;
                    await errorMessage.ShowAsync();
                }
            }
        }
    }
}
